package cn.usts.edu.lesson03;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpFileServerDemo {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(9999);
            Socket socket = serverSocket.accept();//侦听
            InputStream inputStream = socket.getInputStream(); // 文件输入流
            // 读取字节流
            FileOutputStream fileOutputStream = new FileOutputStream("receive.txt");
            int len;
            byte[] buffer=new byte[1024];
            while ((len=inputStream.read(buffer))!=-1){
                fileOutputStream.write(buffer,0,len);
            }


            // 告诉客户端文件传输完毕
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("文件传输完毕".getBytes());

            // 关闭
            outputStream.close();
            fileOutputStream.close();
            inputStream.close();
            socket.close();
            serverSocket.close();



        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
